package com.shop.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shop.constant.UserConstant;
import com.shop.domain.Category;
import com.shop.domain.MessageCenter;
import com.shop.domain.User;
import com.shop.enumeration.MessageStatusEnum;
import com.shop.service.CategoryService;
import com.shop.service.GoodsService;
import com.shop.service.MessageCenterService;
import com.shop.service.UserService;

/**
 * 
 * @author wenjuanhenxing
 * @version V1.0 AdminController.java 2016-2-21 下午12:43:37
 */
@Controller("adminController")
@RequestMapping("/admin")
public class AdminController {
	@Resource(name = "userServiceImpl")
	private UserService userService;

	@Resource(name = "goodsServiceImpl")
	private GoodsService goodsService;

	@Resource(name = "categoryServiceImpl")
	private CategoryService categoryService;

	@Resource(name = "messageCenterServiceImpl")
	private MessageCenterService messageCenterService;

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest request) {
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/error");
		User user = userService.findByUsername(username);
		if (user != null) {
			String realPwd = user.getPassword();
			if (realPwd.equals(password)) {
				request.getSession().setAttribute("admin", user);
				mav.setViewName("admin/adminIndex");
			} else {
				mav.addObject("errors", "密码错误");
			}
		} else {
			mav.addObject("errors", "用户名错误");
		}
		return mav;
	}

	@RequestMapping("/loadCategory")
	public ModelAndView toAddCategoryChild() {
		ModelAndView mav = new ModelAndView();
		List<Category> largeCategories = categoryService.getLargeCategory();
		mav.addObject("largeCategoryList", largeCategories);
		mav.setViewName("admin/addCategoryChild");
		return mav;
	}

	@RequestMapping("/userCertify")
	public ModelAndView certify(HttpServletRequest request) {
		String username = request.getParameter("username");
		User user = userService.findByUsername(username);
		ModelAndView mav = new ModelAndView();
		mav.setViewName("admin/error");
		if (user != null) {
			mav.addObject("user", user);
			mav.setViewName("admin/userCertify");
		}
		return mav;
	}

	@RequestMapping("/passCertify")
	public String passCertify(HttpServletRequest request) {
		String username = request.getParameter("username");
		User user = userService.findByUsername(username);
		user.setCertifyStatus(true);
		userService.updateCertifyStatus(user);
		sendMessage(UserConstant.PASS_CERTIFY, UserConstant.ADMIN_ID,
				user.getId());
		return "admin/adminIndex";
	}

	@RequestMapping("/rejectCertify")
	public String rejectCertify(HttpServletRequest request) {
		String username = request.getParameter("username");
		User user = userService.findByUsername(username);
		sendMessage(UserConstant.REJECT_CERTIFY, UserConstant.ADMIN_ID,
				user.getId());
		return "admin/adminIndex";
	}

	// 发送消息--即消息持久化
	private void sendMessage(String content, Long senderID, Long receiverId) {
		MessageCenter message = new MessageCenter();
		message.setReceiver(receiverId);
		message.setSender(senderID);
		message.setStatus(MessageStatusEnum.UNREAD.getKey());
		message.setContent(content);
		messageCenterService.addMessage(message);

	}

	@RequestMapping("/addLargeCategory")
	public ModelAndView addLargeCategory(HttpServletRequest request)
			throws UnsupportedEncodingException {
		String largeCategory = new String(request.getParameter("largeCategory")
				.getBytes("ISO-8859-1"), "utf-8");
		Category category = new Category();
		category.setName(largeCategory);
		categoryService.addCategory(category);
		ModelAndView mav = new ModelAndView();
		mav.addObject("state", "添加成功");
		mav.setViewName("admin/addCategory");
		return mav;
	}

	@RequestMapping("/addSmallCategory")
	public ModelAndView addSmallCategory(HttpServletRequest request)
			throws UnsupportedEncodingException {
		String smallCategory = new String(request.getParameter("smallCategory")
				.getBytes("ISO-8859-1"), "utf-8");
		Long faId = Long.parseLong(request.getParameter("largeCategoryId"));
		Category category = new Category();
		category.setName(smallCategory);
		category.setParentId(faId);
		categoryService.addCategory(category);
		ModelAndView mav = new ModelAndView();
		mav.addObject("state", "添加成功");
		mav.setViewName("admin/addCategoryChild");
		return mav;
	}
}
